# Copyright (c) 2006 The Pragmatic Programmers, LLC.
# Reproduced from the book "Agile Web Development with Rails, 2nd Ed.",
# published by The Pragmatic Bookshelf.
# Available from www.pragmaticprogrammer.com/titles/rails2
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this source code (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# 1) This Software cannot be used in any training course or seminar, whether
# presented live, via video, audio, screencast, or any other media, without
# explicit prior permission from the publisher.
# 
# 2) The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. 

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def number_to_rmb( number )
    number_to_currency number, :unit=>"￥", :precision=>2
  end
  
  def number_to_usd( number )
    number_to_currency number, :unit=>"$", :precision=>2
  end
  
  def select_customer
    if admin?
      select "order","customer", 
          Customer.find(:all).collect {|c|[c.customer, c.customer]}, 
          {:include_bank => true} 
    else      
      select "order","customer", 
          Customer.find_all_by_user(cur_user_name).collect {|c|[c.customer, c.customer]}, 
          {:include_bank => true} 
    end
  end
  
  def query_by_order_customer
    if admin?
      select "order","customer", 
          Customer.find(:all).collect {|c|[c.customer, c.customer]}.insert(0, ['', nil]), 
          {:include_bank => true} 
    else      
      select "order","customer", 
          Customer.find_all_by_user(cur_user_name).collect {|c|[c.customer, c.customer]}.insert(0, ['', nil]), 
          {:include_bank => true} 
    end
  end
  
  def select_payment
    select "order","payment",['西联汇款','银行汇款','现金支付RMB','现金支付USD']
  end
  
  def select_order_status
    select "order","status",['open','close']
  end
  
  def query_by_order_status
    select "order","status",['', 'open','close']
  end
  
  def query_order_by
    select 'order_by','',
      [
      ['最新时间','created_at DESC'],
      ['最早时间','created_at'],
      ['最大毛利润', 'profit DESC'],
      ['最小毛利润', 'profit'],
      ['最大实际到款', 'sumusd_re DESC'],
      ]
  end
  
  def select_req_charge
    select "order","req_charge",['  ','常规充电器','美规充电器','欧规充电器','澳规充电器']
  end
  
  def cur_user
      User.find_by_name session[:user_name]
  end
  
  def cur_user_name
    session[:user_name]
  end
  
  def admin?
    cur_user.admin?
  end
  
  def print?
    request[:print] == true
  end
end
